/*
 * Copyright (C) 2009 Chang Min Lee <ch5ngmn@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details. (/COPYING)
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#ifndef TAONET_IO_SESSION_MANAGER_H
#define TAONET_IO_SESSION_MANAGER_H

#include "IoEventHandler.h"
#include <list>
#include <map>
class Taonet_Session_i;
class IoSessionManager
{
public:
	IoSessionManager (IoEventHandler *handler, size_t max);
	~IoSessionManager();
	//load all session
	int load();
	void unload(); 
	
	//find session
	virtual Taonet_Session_i * find (ACE_UINT32 key);
	
	//create session
	Taonet_Session_i * create_session (ACE_UINT32 key);

	//destroy session
	void destroy_session (ACE_UINT32 key);
	
	void recv (ACE_UINT32 key, IoEventHandler::MsgType type, const ACE_UINT8 *bytes, ACE_UINT32 length);

	void send (ACE_UINT32 key, const ACE_UINT8 *data, ACE_UINT32 length);

	ACE_UINT16 max_capacity();
	ACE_UINT16 current_size();
protected:

	typedef std::list<Taonet_Session_i*> EVICTOR_QUEUE;
	typedef std::list<Taonet_Session_i*> SESSION_CACHE;
	typedef std::map<ACE_UINT32, EVICTOR_QUEUE::iterator> ACTIVE_OBJECT_MAP;
	
	IoEventHandler *handler_;
	size_t maxSession_;
	EVICTOR_QUEUE evictor_queue_;
	SESSION_CACHE session_cache_;
  ACTIVE_OBJECT_MAP aom_ ;
};

#endif